Dashboard Temp Share Shortlinks Frames API

HTMLify

Majority Element.cpp
Views: 1 | Author: cody
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include<bits/stdc++.h>
using namespace std;
int majorityElement(vector<int> v) {
	// Write your code here
	// Using Hashing approch
	map<int,int>mpp;
	for(int i=0; i<v.size(); i++){
		mpp[v[i]]++;
	}
	for(auto it: mpp){
		if(it.second > v.size() / 2){
			return it.first;
		}
	}
	return -1;
	
}